<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use \Exception;
use think\Log;

class ZlSynastryReportUnlockTimeInfo extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_synastry_report_unlock_time_info';

    public function saveData(array $data, int $startTime, int $endTime)
    {
        try
        {
            $unlockTimeArr = [];
            $resData = [];
            $i = 0;
            do
            {
                $month = date('Ym', strtotime('+' . $i . 'month', $startTime));
                //echo $i . ':' . $month . '<br>';
                $unlockTimeArr[] = $month;
                $data['unlock_time'] = $month;
                $resData[] = $data;
                $i++;
            } while ($month < $endTime);
            $this->saveAll($resData);
        }
        catch (Exception $e)
        {
            Log::write("合盘报告解锁月份数据写入失败data:" . \GuzzleHttp\json_decode($data, JSON_UNESCAPED_UNICODE) . " error:" . $e->getMessage() . '[ ' . $e->getFile() . '：' . $e->getLine() . ' ]', 'error', true);
        }
    }

    /**
     * 获取用户解锁合盘报告月份信息
     * @param int $uid
     * @param int $rid1
     * @param int $rid2
     * @param int $srid
     * @param int $relationId
     * @param int $startDate
     * @param int $endDate
     * @return array
     */
    public function getUnlockMonthInfo(int $uid, int $rid1, int $rid2, int $srid, int $relationId, int $startDate, int $endDate): array
    {
        try
        {
            return $this->where(['uid' => $uid, 'rid1' => $rid1, 'rid2' => $rid2, 'status' => 1, 'srid' => $srid, 'relation_id' => $relationId])
                ->where('unlock_time', '>=', $startDate)
                ->where('unlock_time', '<', $endDate)
                ->distinct('unlock_time')
                ->column('unlock_time');
        }
        catch (Exception $e)
        {
            return [];
        }
    }

}
